home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter7 / editlabl.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  10KB  |  321 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "editlabl.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "App";
  20. LPCTSTR lpszTitle   = "TreeView_EditLabel()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. VOID AddItemsToTree( HWND hTree );
  107.  
  108. #define IMAGESIZE 16
  109.  
  110. LPCTSTR lpszData[4] = { "Circle", "Rectangle", "Cross", "Check" };
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HWND hTree = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE  :
  119.               {
  120.                  HIMAGELIST hImage;
  121.  
  122.                  InitCommonControls();
  123.  
  124.                  hImage   = ImageList_Create( IMAGESIZE, IMAGESIZE, 
  125.                                               ILC_COLOR4 | ILC_MASK, 4, 1 ); 
  126.                  hTree = CreateWindowEx( WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
  127.                                          WS_CHILD | WS_BORDER | WS_VISIBLE | 
  128.                                          WS_VSCROLL | TVS_HASLINES | 
  129.                                          TVS_HASBUTTONS | TVS_LINESATROOT |
  130.                                          TVS_DISABLEDRAGDROP | TVS_EDITLABELS,
  131.                                          10, 100, 100, 100,
  132.                                          hWnd,
  133.                                          (HMENU)1,
  134.                                          hInst,
  135.                                          NULL);
  136.  
  137.                  if ( hTree )
  138.                  {
  139.                     int i;
  140.  
  141.                     // Associate the image list with the tree view.
  142.                     //.............................................
  143.                     TreeView_SetImageList( hTree, hImage, TVSIL_NORMAL );
  144.  
  145.                     // Add the images to the tree view image list.
  146.                     //............................................
  147.                     for ( i=0; i<4; i++ )
  148.                        ImageList_AddIcon( hImage, LoadIcon( hInst, lpszData[i] ) );
  149.  
  150.                     AddItemsToTree( hTree );
  151.                  }
  152.               }
  153.               break;
  154.  
  155.       case WM_SIZE :
  156.               if ( hTree )
  157.                  MoveWindow( hTree, 0, 0, LOWORD( lParam ), HIWORD( lParam ), FALSE );
  158.               break;
  159.  
  160.  
  161.       case WM_NOTIFY :
  162.               if ( wParam == 1 )
  163.               {
  164.                  TV_DISPINFO* pNM = (TV_DISPINFO*)lParam;
  165.  
  166.                  switch ( pNM->hdr.code )
  167.                  {
  168.                     case TVN_BEGINLABELEDIT :
  169.                            {
  170.                               HWND hEdit;
  171.  
  172.                               // Limit the number of characters allowed 
  173.                               // in the edit control of the tree view.
  174.                               //.......................................
  175.                               hEdit = TreeView_GetEditControl( hTree );
  176.                               if ( hEdit )
  177.                                  SendMessage( hEdit, EM_LIMITTEXT, 30, 0 );
  178.                            }
  179.                            break;
  180.  
  181.                     case TVN_ENDLABELEDIT :
  182.                            {
  183.                               // If the text was edited, update the text.
  184.                               //.........................................
  185.                               if ( pNM->item.pszText )
  186.                               {
  187.                                  pNM->item.mask = TVIF_TEXT;
  188.                                  TreeView_SetItem( hTree, &pNM->item ); 
  189.                               }
  190.                            }
  191.                            break;
  192.                  } 
  193.               }
  194.               break;
  195.  
  196.  
  197.       case WM_COMMAND :
  198.               switch( LOWORD( wParam ) )
  199.               {
  200.                  case IDM_RENAME :
  201.                         {
  202.                            HTREEITEM hItem;
  203.  
  204.                            // Rename the selected item.
  205.                            //..........................
  206.                            hItem = TreeView_GetSelection( hTree );
  207.                            if ( hItem )
  208.                               TreeView_EditLabel( hTree, hItem );
  209.                         }
  210.                         break;
  211.  
  212.                  case IDM_COLLAPSE :
  213.                  case IDM_EXPAND :
  214.                         {
  215.                            HTREEITEM hItem;
  216.                            
  217.                            // Finish the editing, canceling changes.
  218.                            //.......................................
  219.                            TreeView_EndEditLabelNow( hTree, TRUE );
  220.  
  221.                            // Expand/collapse the selected item.
  222.                            //...................................
  223.                            hItem = TreeView_GetSelection( hTree );
  224.                            if ( hItem )
  225.                               TreeView_Expand( hTree, hItem, 
  226.                                                LOWORD( wParam ) == IDM_EXPAND ?
  227.                                                TVE_EXPAND : TVE_COLLAPSE );
  228.                         }
  229.                         break;
  230.  
  231.                  case IDM_ABOUT :
  232.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  233.                         break;
  234.  
  235.                  case IDM_EXIT :
  236.                         DestroyWindow( hWnd );
  237.                         break;
  238.               }
  239.               break;
  240.       
  241.       case WM_DESTROY :
  242.               if ( TreeView_GetImageList( hTree, TVSIL_NORMAL ) )
  243.                  ImageList_Destroy( TreeView_GetImageList( hTree, TVSIL_NORMAL ) );
  244.  
  245.               PostQuitMessage(0);
  246.               break;
  247.  
  248.       default :
  249.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  250.    }
  251.  
  252.    return( 0L );
  253. }
  254.  
  255.  
  256. VOID AddItemsToTree( HWND hTree )
  257. {
  258.    int             i, j;
  259.    char            szTmp[40];
  260.    TV_INSERTSTRUCT tv;
  261.    HTREEITEM       hParent;
  262.  
  263.    tv.hInsertAfter = TVI_LAST;
  264.    tv.item.mask    = TVIF_TEXT | TVIF_IMAGE | 
  265.                      TVIF_SELECTEDIMAGE | TVIF_PARAM;
  266.  
  267.    // Add the items to the tree view.
  268.    //................................
  269.    for ( i=0; i<20; i++ )
  270.    {
  271.       wsprintf( szTmp, "%s %d", lpszData[0], i+1 );
  272.  
  273.       tv.hParent      = TVI_ROOT;
  274.       tv.item.pszText = szTmp;
  275.       tv.item.iImage  = 0;
  276.       tv.item.iSelectedImage = 0;
  277.       tv.item.lParam         = 0;
  278.  
  279.       hParent = TreeView_InsertItem( hTree, &tv );
  280.       tv.hParent = hParent;
  281.  
  282.       // Add child items.
  283.       //.................
  284.       for( j=1; j<4; j++ )
  285.       {
  286.          tv.item.pszText = (LPTSTR)lpszData[j];
  287.          tv.item.iImage  = j;
  288.          tv.item.iSelectedImage = j;
  289.          tv.item.lParam         = 1;
  290.  
  291.          TreeView_InsertItem( hTree, &tv );
  292.       }
  293.    }
  294. }
  295.  
  296.  
  297. LRESULT CALLBACK About( HWND hDlg,           
  298.                         UINT message,        
  299.                         WPARAM wParam,       
  300.                         LPARAM lParam)
  301. {
  302.    switch (message) 
  303.    {
  304.        case WM_INITDIALOG: 
  305.                return (TRUE);
  306.  
  307.        case WM_COMMAND:                              
  308.                if (   LOWORD(wParam) == IDOK         
  309.                    || LOWORD(wParam) == IDCANCEL)    
  310.                {
  311.                        EndDialog(hDlg, TRUE);        
  312.                        return (TRUE);
  313.                }
  314.                break;
  315.    }
  316.  
  317.    return (FALSE); 
  318. }
  319.  
  320.  
  321.